﻿using DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class LoginFrom : Form
    {
        private UserService userService = new UserService();
        private List<User> usersList = new List<User>();
        public static LoginFrom loginFrom;
        public static string userAccount;
        public LoginFrom()
        {
            InitializeComponent();
            loginFrom = this;
        }

        private void LoginFrom_Load(object sender, EventArgs e)
        {
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userText = textBoxUserName.Text;
            string pwdText = textBoxPwd.Text;

            if (!(userText != null && userText.Trim().Length > 0) || !(pwdText != null && pwdText.Trim().Length > 0))
            {
                return;
            }

            int rs = Login(userText,pwdText);
            if (rs == 1) 
            {
                this.Hide();
                UserForm userForm = new UserForm();
                userAccount = userText;
                userForm.Show();
            }
            else if(rs == 2)
            {
                this.Hide();
                AdminerForm adminerForm = new AdminerForm();
                userAccount = userText;
                adminerForm.Show();
                //MessageBox.Show(rs == 0 ? "用户不存在" : "密码错误");
            }
            else if(rs==0)
                MessageBox.Show("用户不存在");
            else
                MessageBox.Show("密码错误");
        }

        /* 返回1 成功，返回0 用户名错误 ，返回 -1 密码错误*/
        private int Login(string user, string password)
        {
            return userService.Login(user, password);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            this.Hide();
            RegisterForm registerForm = new RegisterForm();
            registerForm.Show();
        }

        private void btnForgetPwd_Click(object sender, EventArgs e)
        {
            this.Hide();
            ModifyPwdForm modifyPwdForm = new ModifyPwdForm();
            modifyPwdForm.Show();
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
